package com.nmgydy.util;

import com.alibaba.fastjson.JSON;
import com.nmgydy.beans.ResultBean;
import com.nmgydy.model.*;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.hibernate.Session;
import org.hibernate.Transaction;

import java.util.List;

public class BpmnUtil {
    public static void main2(String[] args) {
        String xmlFile = "d:/file/diagram.bpmn";
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(xmlFile);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element rootEl = document.getRootElement();
        Element processEl = rootEl.element("process");
        List taskElList = processEl.elements("task");
        for(Object taskElObj : taskElList){
            Element taskEl = (Element) taskElObj;
            String id = taskEl.attribute("id").getValue();
            String name = taskEl.attribute("name").getValue();
            //System.out.println(id+","+name);
        }

        List sequenceFlowElList = processEl.elements("sequenceFlow");
        for(Object sequenceFlowElObj : sequenceFlowElList){
            Element sequenceFlowEl = (Element) sequenceFlowElObj;
            String id = sequenceFlowEl.attribute("id").getValue();
            String sourceRef = sequenceFlowEl.attribute("sourceRef").getValue();
            String targetRef = sequenceFlowEl.attribute("targetRef").getValue();
            //System.out.println(id+","+sourceRef+","+targetRef);
        }

        Element BPMNDiagramEl = rootEl.element("BPMNDiagram");
        Element BPMNPlaneEl = BPMNDiagramEl.element("BPMNPlane");
        List BPMNShapeElList = BPMNPlaneEl.elements("BPMNShape");
        for(Object BPMNShapeElObj : BPMNShapeElList){
            Element BPMNShapeEl = (Element) BPMNShapeElObj;
            String id = BPMNShapeEl.attribute("id").getValue();
            String bpmnElement = BPMNShapeEl.attribute("bpmnElement").getValue();

            Element boundsEl = BPMNShapeEl.element("Bounds");
            String boundsX = boundsEl.attribute("x").getValue();
            String boundsY = boundsEl.attribute("y").getValue();
            String boundsWidth = boundsEl.attribute("width").getValue();
            String boundsHeight = boundsEl.attribute("height").getValue();

            //System.out.println(id+","+bpmnElement+","+boundsX+","+boundsY+","+boundsWidth+","+boundsHeight);
            Element labelEl = BPMNShapeEl.element("BPMNLabel");
            Element labelBoundsEl = labelEl.element("Bounds");
            if(labelBoundsEl != null){
                String labelBoundsX = boundsEl.attribute("x").getValue();
                String labelBoundsY = boundsEl.attribute("y").getValue();
                String labelBoundsWidth = boundsEl.attribute("width").getValue();
                String labelBoundsHeight = boundsEl.attribute("height").getValue();
                //System.out.println("LabelBounds:"+labelBoundsX+","+labelBoundsY+","+labelBoundsWidth+","+labelBoundsHeight);
            }
        }

        List BPMNEdgeElList = BPMNPlaneEl.elements("BPMNEdge");
        for(Object BPMNEdgeElObj : BPMNEdgeElList) {
            Element BPMNEdgeEl = (Element) BPMNEdgeElObj;
            String id = BPMNEdgeEl.attribute("id").getValue();
            String bpmnElement = BPMNEdgeEl.attribute("bpmnElement").getValue();
            List waypointElList = BPMNEdgeEl.elements("waypoint");
            for(Object waypointElObj : waypointElList) {
                Element waypointEl = (Element) waypointElObj;
                String x = waypointEl.attribute("x").getValue();
                String y = waypointEl.attribute("y").getValue();
                System.out.println(id+","+bpmnElement+","+x+","+y);
            }
        }
    }

    public static void main(String[] args) {
        saveBpmn(null,"1");
    }

    public static void saveBpmn(HibernateDao hibernateDao,String caseType) {
        System.out.println("caseType:"+caseType);
        String xmlFile = "d:/file/diagram"+caseType+".bpmn";
        SAXReader saxReader = new SAXReader();
        Document document = null;
        try {
            document = saxReader.read(xmlFile);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Session session = null;
        Transaction transaction = null;
        try{
            session = hibernateDao.getSessionFactory().openSession();
            transaction = session.beginTransaction();

            session.createSQLQuery("delete from cn_bpmn_edge where caseType='"+caseType+"'").executeUpdate();
            session.createSQLQuery("delete from cn_bpmn_edge_waypoint where caseType='"+caseType+"'").executeUpdate();
            session.createSQLQuery("delete from cn_bpmn_sequence_flow where caseType='"+caseType+"'").executeUpdate();
            session.createSQLQuery("delete from cn_bpmn_shape where caseType='"+caseType+"'").executeUpdate();
            session.createSQLQuery("delete from cn_bpmn_task where caseType='"+caseType+"'").executeUpdate();

            Element rootEl = document.getRootElement();
            Element processEl = rootEl.element("process");
            List taskElList = processEl.elements("task");
            for(Object taskElObj : taskElList){
                Element taskEl = (Element) taskElObj;
                String id = taskEl.attribute("id").getValue();
                String name = taskEl.attribute("name").getValue();
                name = name.replaceAll(" ","");
                name = name.replaceAll("天","");
                name = name.replaceAll("日","");
                String[] nameArr = name.split("-");
                int days = 1;
                if(nameArr.length > 1){
                    days = Integer.valueOf(nameArr[1]);
                }
                name = nameArr[0];
                //System.out.println(id+","+name+","+days);
                BpmnTask task = new BpmnTask();
                task.setElementId(id);
                task.setName(name);
                task.setDays(days);
                task.setCaseType(caseType);
                task.setNodeType("1");
                session.save(task);
            }

            List endEventElList = processEl.elements("endEvent");
            for(Object endEventElObj : endEventElList){
                Element endEventEl = (Element) endEventElObj;
                String id = endEventEl.attribute("id").getValue();
                String name = endEventEl.attribute("name").getValue();
                int days = 0;
                //System.out.println(id+","+name+","+days);
                BpmnTask task = new BpmnTask();
                task.setElementId(id);
                task.setName(name);
                task.setDays(days);
                task.setCaseType(caseType);
                task.setNodeType("2");
                session.save(task);
            }

            List sequenceFlowElList = processEl.elements("sequenceFlow");
            for(Object sequenceFlowElObj : sequenceFlowElList){
                Element sequenceFlowEl = (Element) sequenceFlowElObj;
                String id = sequenceFlowEl.attribute("id").getValue();
                String name = null;
                Attribute nameAttr = sequenceFlowEl.attribute("name");
                if(nameAttr != null){
                    name = nameAttr.getValue();
                }
                String sourceRef = sequenceFlowEl.attribute("sourceRef").getValue();
                String targetRef = sequenceFlowEl.attribute("targetRef").getValue();
                //System.out.println(id+","+sourceRef+","+targetRef);
                BpmnSequenceFlow sequenceFlow = new BpmnSequenceFlow();
                sequenceFlow.setElementId(id);
                sequenceFlow.setName(name);
                sequenceFlow.setSourceRef(sourceRef);
                sequenceFlow.setTargetRef(targetRef);
                sequenceFlow.setCaseType(caseType);
                session.save(sequenceFlow);
            }

            Element BPMNDiagramEl = rootEl.element("BPMNDiagram");
            Element BPMNPlaneEl = BPMNDiagramEl.element("BPMNPlane");
            List BPMNShapeElList = BPMNPlaneEl.elements("BPMNShape");
            for(Object BPMNShapeElObj : BPMNShapeElList){
                Element BPMNShapeEl = (Element) BPMNShapeElObj;
                String id = BPMNShapeEl.attribute("id").getValue();
                String bpmnElement = BPMNShapeEl.attribute("bpmnElement").getValue();

                Element boundsEl = BPMNShapeEl.element("Bounds");
                String boundsX = boundsEl.attribute("x").getValue();
                String boundsY = boundsEl.attribute("y").getValue();
                String boundsWidth = boundsEl.attribute("width").getValue();
                String boundsHeight = boundsEl.attribute("height").getValue();

                BpmnShape shape = new BpmnShape();
                shape.setElementId(id);
                shape.setBpmnElement(bpmnElement);
                shape.setShapeX(boundsX);
                shape.setShapeY(boundsY);
                shape.setShapeWidth(boundsWidth);
                shape.setShapeHeight(boundsHeight);
                shape.setCaseType(caseType);

                //System.out.println(id+","+bpmnElement+","+boundsX+","+boundsY+","+boundsWidth+","+boundsHeight);
                Element labelEl = BPMNShapeEl.element("BPMNLabel");
                if(labelEl != null){
                    Element labelBoundsEl = labelEl.element("Bounds");
                    if(labelBoundsEl != null){
                        String labelBoundsX = labelBoundsEl.attribute("x").getValue();
                        String labelBoundsY = labelBoundsEl.attribute("y").getValue();
                        String labelBoundsWidth = labelBoundsEl.attribute("width").getValue();
                        String labelBoundsHeight = labelBoundsEl.attribute("height").getValue();
                        //System.out.println("LabelBounds:"+labelBoundsX+","+labelBoundsY+","+labelBoundsWidth+","+labelBoundsHeight);
                        shape.setLabelX(labelBoundsX);
                        shape.setLabelY(labelBoundsY);
                        shape.setLabelHeight(labelBoundsHeight);
                        shape.setLabelWidth(labelBoundsWidth);
                    }
                }
                session.save(shape);
            }

            List BPMNEdgeElList = BPMNPlaneEl.elements("BPMNEdge");
            for(Object BPMNEdgeElObj : BPMNEdgeElList) {
                Element BPMNEdgeEl = (Element) BPMNEdgeElObj;
                String id = BPMNEdgeEl.attribute("id").getValue();
                String bpmnElement = BPMNEdgeEl.attribute("bpmnElement").getValue();

                BpmnEdge edge = new BpmnEdge();
                edge.setElementId(id);
                edge.setBpmnElement(bpmnElement);
                edge.setCaseType(caseType);
                Element labelEl = BPMNEdgeEl.element("BPMNLabel");
                if(labelEl != null){
                    Element labelBoundsEl = labelEl.element("Bounds");
                    String labelBoundsX = labelBoundsEl.attribute("x").getValue();
                    String labelBoundsY = labelBoundsEl.attribute("y").getValue();
                    String labelBoundsWidth = labelBoundsEl.attribute("width").getValue();
                    String labelBoundsHeight = labelBoundsEl.attribute("height").getValue();
                    //System.out.println("LabelBounds:"+labelBoundsX+","+labelBoundsY+","+labelBoundsWidth+","+labelBoundsHeight);
                    edge.setLabelX(labelBoundsX);
                    edge.setLabelY(labelBoundsY);
                    edge.setLabelHeight(labelBoundsHeight);
                    edge.setLabelWidth(labelBoundsWidth);
                }
                session.save(edge);

                List waypointElList = BPMNEdgeEl.elements("waypoint");
                for(Object waypointElObj : waypointElList) {
                    Element waypointEl = (Element) waypointElObj;
                    String x = waypointEl.attribute("x").getValue();
                    String y = waypointEl.attribute("y").getValue();
                    //System.out.println(id+","+bpmnElement+","+x+","+y);
                    BpmnEdgeWaypoint waypoint = new BpmnEdgeWaypoint();
                    waypoint.setEdgeId(edge.getId());
                    waypoint.setElementId(id);
                    waypoint.setBpmnElement(bpmnElement);
                    waypoint.setWaypointX(x);
                    waypoint.setWaypointY(y);
                    waypoint.setCaseType(caseType);
                    session.save(waypoint);
                }
            }

            String sql = "update cn_bpmn_task t inner join tpl_task_element e on t.elementId=e.elementId set t.id=e.id";
            session.createSQLQuery(sql).executeUpdate();

            transaction.commit();
        } catch (Exception e) {
            e.printStackTrace();
            if (transaction != null) {
                transaction.rollback();
            }
        } finally {
            session.close();
        }

    }
}
